﻿using Orleans;
using Orleans.Configuration;
using Orleans.Hosting;
using Puppy;
using Puppy.Scheduler;
using System.Net;
using System.Runtime.InteropServices;

namespace Microsoft.Extensions.Hosting
{
    public static class HostingExtensions
    {
        public static IHostBuilder UseOSPlatformService(this IHostBuilder builder)
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                builder.UseWindowsService();
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                builder.UseSystemd();
            }

            return builder;
        }

        public static IHostBuilder UseOrleanService(this IHostBuilder builder)
        {
            builder.UseOrleans(orleansBuilder =>
            {
                orleansBuilder
                    .UseLocalhostClustering()
                    .AddMemoryGrainStorageAsDefault()
                    .Configure<EndpointOptions>(option => option.AdvertisedIPAddress = IPAddress.Loopback)
                    .ConfigureApplicationParts(parts => parts
                        .AddApplicationPart(typeof(SchedulerServerModule).Assembly)
                        .WithReferences()
                    );
            });

            return builder;
        }
    }
}
